handlebars.js ➔ getTemplate   D
last analyzed

Complexity

Conditions 12

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 4.8
c 0
b 0
f 0
cc 12

How to fix   Complexity   

Complexity

Complex classes like handlebars.js ➔ getTemplate often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import HandleBars from 'handlebars';
2
import mdinclude from 'mdinclude';
3
import { isString } from 'myrmidon';
4
5
HandleBars.registerHelper('join', (items = [], sep = ' ') => {
6
    return items.join(sep);
7
});
8
9
HandleBars.registerHelper('lowercase', str => {
10
    return isString(str) ? str.toLowerCase() : '';
11
});
12
13
HandleBars.registerHelper('is', function (value, test, options) {
14
    if (value && value === test) {
15
        return options.fn(this);
16
    }
17
18
    return options.inverse(this);
19
});
20
21
HandleBars.registerHelper('any', function (array, options) {
22
    if (array && array.length > 0) {
23
        return options.fn(this);
24
    }
25
26
    return options.inverse(this);
27
});
28
29
HandleBars.registerHelper('more', function (value, test, options) {
30
    if (value > test) {
31
        return options.fn(this);
32
    }
33
34
    return options.inverse(this);
35
});
36
37
HandleBars.registerHelper('less', function (value, test, options) {
38
    if (value < test) {
39
        return options.fn(this);
40
    }
41
42
    return options.inverse(this);
43
});
44
45
export default HandleBars;
46
47
export function getTemplate(entry) {
48
    const templateText = mdinclude.readFileSync(entry); // eslint-disable-line no-sync
49
50
    return HandleBars.compile(templateText, { noEscape: true });
51
}
52